import logging

import pytest

from resource.pom.camera import Camera
from resource.pom.clock import Clock
from resource.pom.contacts import Contacts
from resource.pom.gallery import Gallery
from resource.pom.keyboard import Keyboard
from resource.pom.memorandum import Memorandum
from resource.pom.mms import MMS
from resource.pom.settings import Settings
from utils.manager import Direction


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device, factory_reset):
        logging.info('步骤1.点击设置')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        logging.info('预期结果1.正常进入设置界面')
        device.assert_current_window(self.app.window_name)

        logging.info('步骤2.点击用户和帐户')
        device.dirc_fling(Direction.Down)
        device.click(*self.app.users_accounts_item)
        logging.info('预期结果2.进入用户和帐户页面')
        device.refresh_layout()
        device.assert_text_exist('用户和帐户')
        device.assert_text_exist('当前登录')

        logging.info('步骤3.点击当前登录')
        device.click_element(device.get_element_by_text('当前登录'))
        logging.info('预期结果3.进入用户列表页面')
        device.refresh_layout()
        device.assert_text_exist('添加用户')
        device.assert_text_exist('添加访客')

        logging.info('步骤4.点击添加用户')
        device.click_element(device.get_element_by_text('添加用户'))
        device.refresh_layout()
        text_input = device.get_element_by_type('TextInput')
        add_btn = device.get_element_by_text('添加')
        device.click_element(text_input)
        device.click(*Keyboard.key_a)
        device.click(*Keyboard.key_b)
        device.click(*Keyboard.key_c)
        device.click(*Keyboard.close)
        device.click_element(add_btn)
        device.wait(5)
        device.refresh_layout()
        cancel_switch = device.get_element_by_text('取消')
        device.click_element(cancel_switch)
        logging.info('预期结果4.用户添加成功')
        device.refresh_layout()
        device.assert_text_exist('abc')

        logging.info('步骤5.点击添加访客')
        device.click_element(device.get_element_by_text('添加访客'))
        device.refresh_layout()
        device.click_element(device.get_element_by_text('确定'))
        device.wait(5)
        logging.info('预期结果5.访客添加成功')
        device.refresh_layout()
        device.assert_text_exist('访客')

        logging.info('步骤6.返回锁屏界面切换到子用户,点击各应用测试基本功能')
        device.press_power_key()
        device.wait(1)
        device.press_power_key()
        device.wait(2)
        # device.refresh_layout()
        # sub_user = device.get_element_by_text('abc')
        # sub_user_x, sub_user_y = device.center_of_element(sub_user)
        # device.click(sub_user_x, sub_user_y - 60)
        device.click(560, 100)
        device.wait(5)
        device.unlock()
        # device.force_stop(self.app.bundle_name)
        # device.wait(1)
        # device.start_ability(self.app.bundle_name, self.app.ability_name)
        # device.wait(1)
        # device.dirc_fling(Direction.Down)
        # device.click(*self.app.users_accounts_item)
        logging.info('预期结果6.各功能的基本功能可以正常操作')
        # device.refresh_layout()
        # device.assert_text_exist('abc')
        self.check_system_app(device)

        logging.info('步骤7.返回锁屏界面切换到访客,点击各应用测试基本功能')
        device.press_power_key()
        device.wait(1)
        device.press_power_key()
        device.wait(2)
        device.refresh_layout()
        # visitor = device.get_element_by_text('访客')
        # visitor_x, visitor_y = device.center_of_element(visitor)
        # device.click(visitor_x, visitor_y - 60)
        device.click(560, 100)
        device.wait(5)
        device.unlock()
        # device.force_stop(self.app.bundle_name)
        # device.wait(1)
        # device.start_ability(self.app.bundle_name, self.app.ability_name)
        # device.wait(1)
        # device.dirc_fling(Direction.Down)
        # device.click(*self.app.users_accounts_item)
        logging.info('预期结果7.各功能的基本功能可以正常操作')
        # device.refresh_layout()
        # device.assert_text_exist('访客')
        self.check_system_app(device)

    def check_system_app(self, device):
        for test_app in [Camera, Clock, Contacts, Gallery, Memorandum, MMS]:
            device.go_home()
            device.start_ability(test_app.bundle_name, test_app.ability_name)
            device.assert_current_window(test_app.window_name)
